from selenium import webdriver
from time import sleep
import csv


def writecsv(item):
    with open('百姓网贵州招工12.22.csv', 'a', encoding='utf-8', newline='') as f:
        wir = csv.writer(f)
        try:
            wir.writerow(item)
        except:
            print('error')


driver = webdriver.Chrome(r'E:\Google\chromedriver.exe')


#  联系人
def contacts():
    try:
        a5 = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[1]/div[2]/div/div[2]/div[8]/label[2]')
        return a5.text
    except:
        return ''


def contacts2():
    try:
        a5 = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[1]/div[2]/div/div[2]/div[7]/label[2]')
        return a5.text
    except:
        return ''


def contacts3():
    try:
        a5 = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[1]/div[2]/div/div[2]/div[2]/label[2]')
        return a5.text
    except:
        return ''


def contacts4():
    try:
        a5 = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[1]/div[2]/div/div[2]/div[3]/label[2]')
        return a5.text
    except:
        return ''


#  招工信息
def getInfo():
    try:
        a3 = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[1]/div[3]/section/div[3]')
        return a3.text
    except:
        return ''


def getInfo2():
    try:
        a3 = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[1]/div[3]/section/div[2]')
        return a3.text
    except:
        return ''


def getInfo3():
    try:
        a3 = driver.find_element_by_xpath('//*[@id="agent-company-tab-1"]/div[2]')
        return a3.text
    except:
        return ''


def getInfo4():
    try:
        a3 = driver.find_element_by_xpath('//*[@id="agent-company-tab-1"]/div[1]/p')
        return a3.text
    except:
        return ''


# 工种
def type():
    try:
        a2 = driver.find_element_by_xpath('/html/body/div[1]/ul/li[4]/a')
        return a2.text
    except:
        return ''


def type2():
    try:
        a2 = driver.find_element_by_xpath('/html/body/div[1]/ul/li[3]/a')
        return a2.text
    except:
        return ''


#  地址
def address():
    try:
        a9 = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[1]/div[2]/div/div[2]/div[2]/span')
        return a9.text
    except:
        return ''


def address2():
    try:
        a9 = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[1]/div[2]/div/div[2]/div[1]/span')
        return a9.text
    except:
        return ''


def getElement():
    driver.get('https://hangzhou.baixing.com/chushi/')
    sleep(1)
    w = 380
    for i in range(1, 116):
        try:
            driver.get('https://guizhou.baixing.com/wenmi/m177806/')
            sleep(1)
            driver.execute_script('window.scrollTo(0,' + str(w) + ');')  # 屏幕滑动
            w = w + 110
            sleep(4)
            driver.find_element_by_xpath('//*[@id="all-list"]/ul/li[' + str(i) + ']/div[1]/div/a[1]').click()
            #  操作新开窗口
            windows = driver.window_handles
            driver.switch_to.window(windows[-1])
            a1 = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[1]/div[1]/div[1]/h1')  # 招工标题

            # 工种
            a2 = type()
            ans5 = ''
            a2text1 = type()
            a2text2 = type2()
            if a2text1 != '':
                ans5 = a2text1
            elif a2text2 != '':
                ans5 = a2text2
            else:
                ans5 = "2个都没拿到"

            # 招工信息
            a3 = getInfo()
            ans4 = ''
            a3Text1 = getInfo()
            a3Text2 = getInfo2()
            a3Text3 = getInfo3()
            a3Text4 = getInfo4()
            if a3Text1 != '':
                ans4 = a3Text1
            elif a3Text2 != '':
                ans4 = a3Text2
            elif a3Text3 != '':
                ans4 = a3Text3
            elif a3Text4 != '':
                ans4 = a3Text4
            else:
                ans4 = "四个都没拿到"

            # 时间
            time = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[1]/div[1]/div[2]/div/span[2]')
            """/html/body/div[1]/div[1]/div[1]/div[1]/div[2]/div/span[2]"""
            time_text = time.text
            time_list = time_text.split('日')
            ti1 = time_list[0]
            ti2 = "2021年"
            ti = str(ti2) + str(ti1)
            # 联系人
            a5 = contacts2()
            ans3 = ''
            a5text1 = contacts()
            a5text2 = contacts2()
            a5text3 = contacts3()
            a5text4 = contacts4()
            if a5text2 != '':
                ans3 = contacts2()
            elif a5text1 != '':
                ans3 = contacts()
            elif a5text3 != '':
                ans3 = contacts3()
            elif a5text4 != '':
                ans3 = contacts4()
            else:
                ans3 = "四个都没拿到"

            # 点击查看联系电话
            driver.find_element_by_xpath(
                '/html/body/div[1]/div[1]/div[1]/div[2]/div/section/ul/li[1]/a[2]').click()
            sleep(1)

            # 点击关闭
            driver.find_element_by_xpath(
                '//*[@id="fillresume"]/div/div/div[1]/button').click()
            sleep(1)

            # 联系电话
            a8 = driver.find_element_by_xpath(
                '/html/body/div[1]/div[1]/div[1]/div[2]/div/section/ul/li[1]/a[1]')

            # 工作地址
            a9 = address()
            ans2 = ''
            a9text1 = address()
            a9text2 = address2()
            if a9text1 != '':
                ans2 = address()
            elif a9text2 != '':
                ans2 = address2()
            else:
                ans2 = "两个都没拿到"

            sheng = '贵州'  # 省
            shi = ''  # 市
            xian = ''  # 县

            list = [a1.text, ans5, ans4, ti, ans3, a8.text, sheng, shi, xian, ans2]

            print(list)
            writecsv(list)
        except:
            print('NoSuchElementException')


if __name__ == "__main__":
    getElement()
